package com.logger.export.html;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;

import com.logger.LogFile;
import com.logger.LogFiles;
import com.logger.LogQuery;
import com.logger.LogTransaction;

public class LogUsersHTML {

    public static void main(final String[] args) throws IOException {
        // Directory path here

        final String directoryPath = "C:\\Users\\bbarhoumi\\Desktop\\stageOmar\\2013-08-27\\node2";
        final LogFiles logUsers = new LogFiles();
        logUsers.loadUserFiles(directoryPath);

        final StringBuilder sbHTML = new StringBuilder();
        sbHTML.append("<HTML>");
        sbHTML.append("<HEAD>");
        sbHTML.append("</HEAD>");
        sbHTML.append("<BODY>");

        final List<LogFile> lstLogFile = logUsers;
        for (final LogFile logFile : lstLogFile) {
            final String userName = logFile.getUserName();
            final Date date = logFile.getDate();
            sbHTML.append("<H1>");
            sbHTML.append(userName);
            sbHTML.append("</H1>");
            sbHTML.append("<H2>");
            sbHTML.append(date);
            sbHTML.append("</H2>");
            final List<LogTransaction> listLogTransaction = logFile.getListLogTransaction();
            for (final LogTransaction logTransaction2 : listLogTransaction) {
                sbHTML.append("<BR>");
                final long id = logTransaction2.getId();
                final Date openDateTime = logTransaction2.getOpenDateTime();
                final Date closeDateTime = logTransaction2.getCloseDateTime();
                sbHTML.append("<H3>");
                sbHTML.append("ID Transaction : " + id);
                sbHTML.append("</H3>");
                sbHTML.append("<BR>");
                sbHTML.append("<H3>");
                sbHTML.append(openDateTime);
                sbHTML.append("</H3>");
                sbHTML.append("<BR>");
                sbHTML.append("<H3>");
                sbHTML.append(closeDateTime);
                sbHTML.append("</H3>");
                LogUsersHTML.listQueriesWithTransactions(sbHTML, logTransaction2);
            }

            LogUsersHTML.listQueriesWithoutTransactions(sbHTML, logFile);
        }
        sbHTML.append("</BODY>");
        sbHTML.append("</HTML>");

        PrintStream out = null;
        try {
            out = new PrintStream(new FileOutputStream("C:\\Users\\bbarhoumi\\Desktop\\stageOmar\\test.html"));
            out.append(sbHTML);
            out.flush();
            Runtime.getRuntime().exec("C:\\Users\\bbarhoumi\\Desktop\\stageOmar\\test.html");
        } finally {
            if (out != null) {
                out.close();
            }
        }
        System.out.println(sbHTML);

    }

    public static void listQueriesWithTransactions(final StringBuilder sbHTML, final LogTransaction logTransaction) {
        sbHTML.append("<br>");
        sbHTML.append("Liste des Requetes dans la transaction:");
        sbHTML.append("<TABLE border='3'>");
        sbHTML.append("<TR>");
        sbHTML.append("<TH>");
        sbHTML.append("Date");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Query Name");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Query Content");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Execution Time");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("constructionTime");
        sbHTML.append("</TH>");
        sbHTML.append("</TR>");

        final List<LogQuery> listLogQuery2 = logTransaction.getQueries();
        for (final LogQuery logQuery2 : listLogQuery2) {
            if (logQuery2.getExecutionTime() > 40) {
                sbHTML.append("<TR bgcolor='#FF0000'>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getDateTime());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getQueryName());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getRawQueryContentPreview());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getExecutionTime());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getConstructionTime());
                sbHTML.append("</TD>");
                sbHTML.append("</TR>");
            } else {
                sbHTML.append("<TR>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getDateTime());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getQueryName());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getRawQueryContentPreview());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getExecutionTime());
                sbHTML.append("</TD>");
                sbHTML.append("<TD>");
                sbHTML.append(logQuery2.getConstructionTime());
                sbHTML.append("</TD>");
                sbHTML.append("</TR>");
            }
        }
        sbHTML.append("</TABLE>");
    }

    public static void listQueriesWithoutTransactions(final StringBuilder sbHTML, final LogFile logFile) {
        sbHTML.append("<br>");
        sbHTML.append("Liste des Requetes dans le fichier:");
        sbHTML.append("<TABLE border='3'>");
        sbHTML.append("<TR>");
        sbHTML.append("<TH>");
        sbHTML.append("Date");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Query Name");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Query Content");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("Execution Time");
        sbHTML.append("</TH>");
        sbHTML.append("<TH>");
        sbHTML.append("constructionTime");
        sbHTML.append("</TH>");
        sbHTML.append("</TR>");

        final List<LogQuery> listLogQuery2 = logFile.getListLogQuery();
        for (final LogQuery logQuery2 : listLogQuery2) {
            if (logQuery2.getTransaction() == null) {
                if (logQuery2.getExecutionTime() > 40) {
                    sbHTML.append("<TR bgcolor='#FF0000'>");
                    sbHTML.append("<TR>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getDateTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getQueryName());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getRawQueryContentPreview());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getExecutionTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getConstructionTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("</TR>");
                } else {
                    sbHTML.append("<TR>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getDateTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getQueryName());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getRawQueryContentPreview());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getExecutionTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("<TD>");
                    sbHTML.append(logQuery2.getConstructionTime());
                    sbHTML.append("</TD>");
                    sbHTML.append("</TR>");

                }
            }
        }
        sbHTML.append("</TABLE>");
    }
}